<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: hash_function.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>hash_function.h</h1><a href="hash__function_8h.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef HASH_FUNCTION_H</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define HASH_FUNCTION_H</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="comment">/* hash function from http://www.azillionmonkeys.com/qed/hash.html */</span>
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <span class="preprocessor">#include "stdint.h"</span>             <span class="comment">/* Replace with &lt;stdint.h&gt; if appropriate */</span>
<a name="l00007"></a>00007 
<a name="l00008"></a>00008 <span class="preprocessor">#undef get16bits</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#if (defined(__GNUC__) &amp;&amp; defined(__i386__)) || defined(__WATCOMC__) \</span>
<a name="l00010"></a>00010 <span class="preprocessor">          || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)</span>
<a name="l00011"></a>00011 <span class="preprocessor"></span><span class="preprocessor">#define get16bits(d) (*((const uint16_t *) (d)))</span>
<a name="l00012"></a>00012 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00013"></a>00013 <span class="preprocessor"></span>
<a name="l00014"></a>00014 <span class="preprocessor">#if !defined (get16bits)</span>
<a name="l00015"></a><a class="code" href="hash__function_8h.html#bc7d71657be8975a51684e41029b7964">00015</a> <span class="preprocessor"></span><span class="preprocessor">#define get16bits(d) ((((const uint8_t *)(d))[1] &lt;&lt; UINT32_C(8))\</span>
<a name="l00016"></a>00016 <span class="preprocessor">                                      +((const uint8_t *)(d))[0])</span>
<a name="l00017"></a>00017 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00018"></a>00018 <span class="preprocessor"></span>
<a name="l00019"></a><a class="code" href="hash__function_8h.html#d1a1b70cff8b395958e4f62f1f02f1bc">00019</a> uint32_t <a class="code" href="hash__function_8h.html#d1a1b70cff8b395958e4f62f1f02f1bc">__hash</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *data, <span class="keywordtype">int</span> len)
<a name="l00020"></a>00020 {
<a name="l00021"></a>00021         uint32_t <a class="code" href="hash-afinidades_8h.html#62c7e0fab503d9c79c83dcccfc64a7b3" title="Funcao HASH.">hash</a> = 0, tmp;
<a name="l00022"></a>00022         <span class="keywordtype">int</span> rem;
<a name="l00023"></a>00023 
<a name="l00024"></a>00024         <span class="keywordflow">if</span> (len &lt;= 0 || data == NULL)
<a name="l00025"></a>00025                 <span class="keywordflow">return</span> 0;
<a name="l00026"></a>00026 
<a name="l00027"></a>00027         rem = len &amp; 3;
<a name="l00028"></a>00028         len &gt;&gt;= 2;
<a name="l00029"></a>00029 
<a name="l00030"></a>00030         <span class="comment">/* Main loop */</span>
<a name="l00031"></a>00031         <span class="keywordflow">for</span> (; len &gt; 0; len--) {
<a name="l00032"></a>00032                 hash += <a class="code" href="hash__function_8h.html#bc7d71657be8975a51684e41029b7964">get16bits</a>(data);
<a name="l00033"></a>00033                 tmp = (<a class="code" href="hash__function_8h.html#bc7d71657be8975a51684e41029b7964">get16bits</a>(data + 2) &lt;&lt; 11) ^ hash;
<a name="l00034"></a>00034                 hash = (hash &lt;&lt; 16) ^ tmp;
<a name="l00035"></a>00035                 data += 2 * <span class="keyword">sizeof</span>(uint16_t);
<a name="l00036"></a>00036                 hash += hash &gt;&gt; 11;
<a name="l00037"></a>00037         }
<a name="l00038"></a>00038 
<a name="l00039"></a>00039         <span class="comment">/* Handle end cases */</span>
<a name="l00040"></a>00040         <span class="keywordflow">switch</span> (rem) {
<a name="l00041"></a>00041         <span class="keywordflow">case</span> 3:
<a name="l00042"></a>00042                 hash += <a class="code" href="hash__function_8h.html#bc7d71657be8975a51684e41029b7964">get16bits</a>(data);
<a name="l00043"></a>00043                 hash ^= hash &lt;&lt; 16;
<a name="l00044"></a>00044                 hash ^= data[<span class="keyword">sizeof</span>(uint16_t)] &lt;&lt; 18;
<a name="l00045"></a>00045                 hash += hash &gt;&gt; 11;
<a name="l00046"></a>00046                 <span class="keywordflow">break</span>;
<a name="l00047"></a>00047         <span class="keywordflow">case</span> 2:
<a name="l00048"></a>00048                 hash += <a class="code" href="hash__function_8h.html#bc7d71657be8975a51684e41029b7964">get16bits</a>(data);
<a name="l00049"></a>00049                 hash ^= hash &lt;&lt; 11;
<a name="l00050"></a>00050                 hash += hash &gt;&gt; 17;
<a name="l00051"></a>00051                 <span class="keywordflow">break</span>;
<a name="l00052"></a>00052         <span class="keywordflow">case</span> 1:
<a name="l00053"></a>00053                 hash += *data;
<a name="l00054"></a>00054                 hash ^= hash &lt;&lt; 10;
<a name="l00055"></a>00055                 hash += hash &gt;&gt; 1;
<a name="l00056"></a>00056         }
<a name="l00057"></a>00057 
<a name="l00058"></a>00058         <span class="comment">/* Force "avalanching" of final 127 bits */</span>
<a name="l00059"></a>00059         hash ^= hash &lt;&lt; 3;
<a name="l00060"></a>00060         hash += hash &gt;&gt; 5;
<a name="l00061"></a>00061         hash ^= hash &lt;&lt; 2;
<a name="l00062"></a>00062         hash += hash &gt;&gt; 15;
<a name="l00063"></a>00063         hash ^= hash &lt;&lt; 10;
<a name="l00064"></a>00064 
<a name="l00065"></a>00065         <span class="keywordflow">return</span> hash;
<a name="l00066"></a>00066 }
<a name="l00067"></a>00067 
<a name="l00068"></a>00068 <span class="preprocessor">#endif</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:25 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
